﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using SHDocVw;
using mshtml;

namespace BindingIEBrowser
{
    public class BindingCore
    {
        //public IList<IWebBrowser> InstanceList
        //{
        //    get { return InstanceList; }
        //    //set { this.InstanceList = value; }
        //}

        private static ShellWindows ShellWindows
        {
            get { return new ShellWindowsClass();}
        }

        private IList<IEInstanceItems> _instanceList;

        private HTMLDocument _htmlDocument;

        public IList<HDFormItems> GetHDForms()
        {
            IList<HDFormItems> hdFormItemsesList = new List<HDFormItems>();
            var tt = this._htmlDocument.frames.item(1) as HTMLWindow2Class;
         
            for(var i =0;i<this._htmlDocument.forms.length;i++)
            {
                var hfe = this._htmlDocument.forms.item(i) as HTMLFormElementClass;
                hdFormItemsesList.Add(new HDFormItems());
            }
            return hdFormItemsesList;
        }
        public IList<HDFrameItems> GetHDFrames()
        {
            //IList<HDFormItems> hdFormItemsesList = new List<HDFormItems>();
            var frameItemses = new List<HDFrameItems>();

            //Level 1 0:menu_frm 1:data_frm
            for (var i = 0; i < this._htmlDocument.frames.length; i++)
            {
                var hfe = this._htmlDocument.frames.item(i) as HTMLWindow2Class;

                    //frameItemses.Add(new HDFrameItems(hfe));
                if ("data_frm".Equals(hfe.name))
                {
                    //Level 2 

                    for (var j = 0; j < hfe.document.frames.length; j++)
                    {
                        try
                        {

                            var hfe2 = hfe.document.frames.item(j) as HTMLWindow2Class;
                            frameItemses.Add(new HDFrameItems(hfe2));
                        }catch(Exception e)
                        {
                            throw new Exception("-" + j + "-" + hfe.document.frames.length);
                        }
                    }
                }

            }
            return frameItemses;
        }
        public bool GetIEInstances(IEInstanceItems pIEInstanceItems)
        {

            for (var i = 0; i < ShellWindows.Count; i++)
            {

                var wb = ShellWindows.Item(i) as IWebBrowser;
                
                if (wb == null)
                    continue;
                try
                {
                    if (
                        wb.LocationName.EndsWith(pIEInstanceItems.LocationName)
                        && wb.LocationURL.EndsWith(pIEInstanceItems.LocationURL)
                        )
                        {
                        //OVERRIDE EQUALS //wb.GetHashCode()
                        //if (wb.Equals(pIEInstanceItems))
                        //{
                        //    return;
                        //}

                        // return wb.Document as HTMLDocument;//the first object
                        this._htmlDocument = wb.Document as HTMLDocument;
                        //HTMLFormElement hfe = new HTMLFormElementClass();
                        //hfe.
                        //hd.forms.GetEnumerator()
                        
                    }
                }
                catch (Exception)
                {

                    continue;//??
                }

            }

            return this._htmlDocument != null;
            //return null;
        }

        public IList<IEInstanceItems> GetIEInstances()
        {

            this._instanceList = new List<IEInstanceItems>();
            for (int i = 0; i < ShellWindows.Count; i++)
            {

                var wb = ShellWindows.Item(i) as IWebBrowser;//IWebBrowserApp;//IWebBrowser2;//
               
                if(wb == null)
                    continue;
                try
                {
                    //PropertyInfo[] aryPI = wb.GetType().GetProperties();


                    this._instanceList.Add(new IEInstanceItems(wb));
                }
                catch (Exception)
                {
                    continue;
                }
            }

            if (this._instanceList.Count == 0)
                this._instanceList = null;
            return this._instanceList;
        }

        private IEnumerator<HTMLWindow2Class> GetFrames(HTMLDocumentClass doc)
        {
            var length = doc.frames.length;
            for(var i = 0;i<length;i++)
            {
                yield return doc.frames.item(i) as HTMLWindow2Class;
            }
        }

        private void Test()
        {
            HTMLWindow2Class we = new HTMLWindow2Class();
            //we.GetType().get
            //this.GetFrames(null);
        }
    }
}
